{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run 7.2.3-perceptron.ipynb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x  |  y  |  x AND y\n",
      "--------------------\n",
      "0  |  0  |    0\n",
      "0  |  1  |    0\n",
      "1  |  0  |    0\n",
      "1  |  1  |    1\n"
     ]
    }
   ],
   "source": [
    "# Logical AND\n",
    "X = torch.tensor([[0., 0.],\n",
    "                 [0., 1.],\n",
    "                 [1., 0.],\n",
    "                 [1., 1.]], dtype=torch.float32)\n",
    "W = torch.tensor([[1.0, 1.0]], dtype=torch.float32)\n",
    "b = torch.tensor([-1.5])\n",
    "Y = Perceptron(X=X, W=W, b=b, activation=torch.heaviside)\n",
    "print('x  |  y  |  x AND y')\n",
    "print('--------------------')\n",
    "for x, y in zip(X, Y):\n",
    "    print('{}  |  {}  |    {}'.format(int(x[0]), int(x[1]), int(y[0])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x  |  y  |  x OR y\n",
      "--------------------\n",
      "0  |  0  |     0\n",
      "0  |  1  |     1\n",
      "1  |  0  |     1\n",
      "1  |  1  |     1\n"
     ]
    }
   ],
   "source": [
    "# Logical OR\n",
    "X = torch.tensor([[0., 0.],\n",
    "                 [0., 1.],\n",
    "                 [1., 0.],\n",
    "                 [1., 1.]], dtype=torch.float32)\n",
    "W = torch.tensor([[1.0, 1.0]], dtype=torch.float32)\n",
    "b = torch.tensor([-0.5])\n",
    "Y = Perceptron(X=X, W=W, b=b, activation=torch.heaviside)\n",
    "print('x  |  y  |  x OR y')\n",
    "print('--------------------')\n",
    "for x, y in zip(X, Y):\n",
    "    print('{}  |  {}  |     {}'.format(int(x[0]), int(x[1]), int(y[0])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x  |  NOT x\n",
      "-----------\n",
      "0  |  1\n",
      "1  |  0\n"
     ]
    }
   ],
   "source": [
    "# Logical NOT\n",
    "X = torch.tensor([[0],\n",
    "                 [1.]\n",
    "                 ], dtype=torch.float32)\n",
    "W = torch.tensor([[-1.0]], dtype=torch.float32)\n",
    "b = torch.tensor([0.5])\n",
    "Y = Perceptron(X=X, W=W, b=b, activation=torch.heaviside)\n",
    "print('x  |  NOT x')\n",
    "print('-----------')\n",
    "for x, y in zip(X, Y):\n",
    "    print('{}  |  {}'.format(int(x[0]), int(y[0])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x  |  y  |  x OR y\n",
      "--------------------\n",
      "0  |  0  |     0\n",
      "0  |  1  |     1\n",
      "1  |  0  |     1\n",
      "1  |  1  |     0\n"
     ]
    }
   ],
   "source": [
    "# Logical XOR\n",
    "X = torch.tensor([[0., 0.],\n",
    "                 [0., 1.],\n",
    "                 [1., 0.],\n",
    "                 [1., 1.]], dtype=torch.float32)\n",
    "W0 = torch.tensor([[1.0, -1.0],\n",
    "                  [-1.0, 1.0]], dtype=torch.float32)\n",
    "b0 = torch.tensor([-0.5, -0.5])\n",
    "W1 = torch.tensor([[1.0, 1.0]], dtype=torch.float32)\n",
    "b1 = torch.tensor([-0.5])\n",
    "\n",
    "Y = MLP(X=X, W0=W0, W1=W1, b0=b0, b1=b1, \n",
    "        activation0=torch.heaviside, activation1=torch.heaviside)\n",
    "print('x  |  y  |  x OR y')\n",
    "print('--------------------')\n",
    "for x, y in zip(X, Y):\n",
    "    print('{}  |  {}  |     {}'.format(int(x[0]), int(x[1]), int(y[0])))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
